<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
          <template slot="menuLeft">
            <el-button type="warning"
                       size="small"
                       plain
                       icon="el-icon-download"
                       @click="handleExport">导出
            </el-button>
          </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {statStation} from "@/api/ems/vrptstatstation";
  import {getDeptTree} from "@/api/system/dept";
  import {mapGetters} from "vuex";
  import website from '@/config/website';
  import {getToken} from '@/util/auth'

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 10
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          menu: false,
          page: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "所属车队",
              prop: "deptId",
              type: "tree",
              multiple: false,
              search:true,
              defaultExpandAll: true,//默认展示
              dicData: [],
              hide: true,
              display: false,
              props: {
                label: "title"
              },
            },
            {
              label: "统计日期",
              prop: "statDate",
              type: "date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd", 
              search:true,
              hide: true,
              display: false,
            },
            {
              label: "站点ID",
              prop: "stationId",
            },
            {
              label: "站点名称",
              prop: "stationName",
            },
            {
              label: "车辆数",
              prop: "carCnt",
            },
            {
              label: "座位数",
              prop: "vehicleSeats",
            },
            {
              label: "总载人数",
              prop: "total",
            },
            {
              label: "载客人数",
              prop: "allCnt",
            },
            {
              label: "全票人数",
              prop: "payCnt",
            },
            {
              label: "半票人数",
              prop: "halfCnt",
            },
            {
              label: "免票人数",
              prop: "freeCnt",
            },
          ]
        },
        data: []
      };
    },
    mounted() {
      // 非租户模式默认加载管理组数据
      if (!website.tenantMode) {
         this.initData(website.tenantId)
      }
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      initData(tenantId) {
        getDeptTree(tenantId).then(res => {
          const column = this.findObject(this.option.column, "deptId");
          column.dicData = res.data.data;
        });
      },      
      handleExport() {
        this.$confirm("是否导出统计数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          window.open(`/emsapi/blade-ems/export-station?${this.website.tokenHeader}=${getToken()}&deptId=${this.search.deptId}&statDate=${this.search.statDate}`);
        });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        statStation(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = 100;//不分页
          this.data = data;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
